package cn.zhangjie.user.controller;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.system.query.DetailQuery;
import cn.zhangjie.user.domain.Logininfo;
import cn.zhangjie.user.dto.LoginDto;
import cn.zhangjie.user.dto.WxDto;
import cn.zhangjie.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logininfo")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;
    //微信登录
    @GetMapping("/weChat/{code}")
    public JsonResult weChat(@PathVariable("code") String code){
        System.out.println(11111);
        System.out.println(code.toString());
        try {
            //service直接返回一个JsonResult
            return logininfoService.weChat(code);

        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }

    @PostMapping("/weChat/binder")
    public JsonResult binder(@RequestBody WxDto dto){
        try {
            //service直接返回一个JsonResult
            return logininfoService.binder(dto);

        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }






    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map =  logininfoService.account(dto);
            return JsonResult.me().setResultObj(map);
        }catch (BusinssException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常，稍后重试");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Logininfo> findall(){
        return logininfoService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    public Logininfo findone(@PathVariable("id") Long id){
        return logininfoService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            logininfoService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updataoradd(@RequestBody Logininfo logininfo){

        try {
            if(logininfo.getId()==null){
                logininfoService.add(logininfo);
                return JsonResult.me();
            }else {
                logininfoService.update(logininfo);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Logininfo> pagequery(@RequestBody DetailQuery detailQuery){
        try {
            return logininfoService.queryPage(detailQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            logininfoService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }







}
